/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.core.HashCodeFactory;
import com.bourre.data.collections.Map;
import com.bourre.data.iterator.Iterator;

import fever.log.FeverDebug;
import fever.utils.StringUtil;

/**
 * File structure.
 * 
 * @author Romain Ecarnot
 */
class fever.data.file.File
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _m : Map = new Map();
	
	private var _nID : Number;	private var _sName : String;	private var _sParentPath : String;	private var _dCreation : Date;	private var _dModified : Date;
	private var _bDirectory : Boolean;
	private var _nSize : Number;	private var _sExtension : String;
	private var _oData : Object;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Not use. Enable array sorting on file's name. */
	public var nameField : String;
	
	/** Not use. Enable array sorting on file's type ( folder / file ). */
	public var typeField : Number;	
	
	
	//-------------------------------------------------------------------------
	// File Manager API
	//-------------------------------------------------------------------------
	
	/**
	 * Creates {@link File} using passed-in {@code pathname} as full qualified 
	 * file path.
	 * 
	 * @return File instance 
	 */
	public static function create( pathname : String, isFolder : Boolean ) : File
	{
		if( !pathname || pathname.length < 2 ) 
		{
			FeverDebug.ERROR( 'Must pass a valid valid file name' );
			return null;
		}
		
		var file : File = retreiveFile( pathname );
		
		if( !file ) file = new File( pathname, isFolder );
		
		return file;
	}
	
	/**
	 * Returns {@code true} if a file is registred with passed-in 
	 * {@code key} identifier. ( otherwise returns {@code false} ).
	 */
	public static function isRegistered( fileID : Number ) : Boolean
	{
		return _m.containsKey( fileID );
	}
	
	/**
	 * Returns {@link fever.data.file.IFile} registred 
	 * with passed-in {@code key} id.
	 */
	public static function getFile( fileID : Number ) : File
	{
		if ( isRegistered( fileID ) ) return _m.get( fileID );
		return null;
	}
	
	/**
	 * Returns {@link fever.data.file.IFile} registred 
	 * with passed-in {@code key} id.
	 */
	public static function retreiveFile( pathname : String ) : File
	{
		pathname = StringUtil.trim( pathname );
		pathname = StringUtil.rightTrim( pathname, '/' );
		pathname = StringUtil.startWithChar( pathname, '/', true );
		
		var iterator : Iterator = _m.getValuesIterator();
		
		while( iterator.hasNext() )
		{
			var o : File = iterator.next();
			
			if( ( o.getPath() ) == pathname ) return o;	
		}
		
		return null;
	}
	
	
	//-------------------------------------------------------------------------
	// File API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns file's name.
	 */
	public function getName( ) : String
	{
		return _sName + ( getExtension() ? ( '.' + getExtension() ) : '' );	
	}
	
	/**
	 * 
	 */
	public function getNameOnly(  ) : String
	{
		return _sName;
	}
	
	/**
	 * 
	 */
	public function setFullQualifiedPath( fullPathName : String ) : Void
	{
		_setFullQualifiedPath( fullPathName );	
	}
	
	/**
	 * Returns file's path.
	 */
	public function getPath( ) : String
	{
		var separator : String = ( _sParentPath == '/' ) ? '' : '/';
		
		return _sParentPath + separator + getName(); 
	}
	
	/**
	 * Sets the creation file's date.
	 * 
	 * @param creationDate Creation date
	 */
	public function setCreatedDate( creationDate : Date ) : Void
	{
		if( creationDate ) _dCreation = creationDate;
	}
	
	/**
	 * Returns file's creation date.
	 */
	public function getCreatedDate(  ) : Date
	{
		return _dCreation;
	}
	
	/**
	 * Sets the modification file's date.
	 * 
	 * @param creationDate Creation date
	 */
	public function setModifiedDate( modificationDate : Date ) : Void
	{
		if( modificationDate ) _dModified = modificationDate;
	}
	
	/**
	 * Returns file's modification date.
	 */
	public function getModifiedDate(  ) : Date
	{
		return _dModified;
	}
	
	/**
	 * Sets if file is a directory {@code true} or not {@code false}
	 */
	public function setDirectory( isFolder : Boolean ) : Void
	{
		_bDirectory = isFolder;
		typeField = _bDirectory ? 0 : 1;
	}
	
	/** 
	 * Returns {@code true} if file is a directory.
	 */
	public function isDirectory(  ) : Boolean
	{
		return _bDirectory;
	}
	
	/** 
	 * Returns {@code true} if file is not a directory.
	 */
	public function isFile(  ) : Boolean
	{
		return !_bDirectory;
	}
	
	/**
	 * Sets the file's size
	 * 
	 * @param fileSize File's size in octets
	 */
	public function setSize( fileSize : Number ) : Void
	{
		if( fileSize ) _nSize = fileSize;
	}
	
	/**
	 * Returns file's size.
	 */
	public function getSize(  ) : Number
	{
		return _nSize;
	}
	
	/**
	 * Defines file extension.
	 */
	public function setExtension( fileExtension : String ) : Void
	{
		_sExtension = fileExtension;
	}
	
	/**
	 * Returns file's extension
	 */
	public function getExtension(  ) : String
	{
		return _sExtension.toLowerCase();
	}
	
	/**
	 * Sets file's content.
	 */
	public function setContent( fileData ) : Void
	{
		_oData = fileData;
	}
	
	/**
	 * Returns file's content
	 */
	public function getContent(  ) 
	{
		return _oData;	
	}
	
	/**
	 * 
	 */
	public function getParentPath(  ) : String
	{
		return _sParentPath;
	}
	
	/**
	 * Returns file's parent folder.
	 */
	public function getParent(  ) : File
	{
		return _getParent( getParentPath() );
	}
	
	/**
	 * 
	 */
	public function getChildList(  ) : Array
	{
		if( isFile() ) return null;
		
		return _getChildList( this );
	}
	
	/**
	 * Returns unique file's id.
	 */
	public function getID() : Number
	{
		return _nID;	
	}
	
	/**
	 * 
	 */
	public function equals( file : File ) : Boolean
	{
		return getPath() == file.getPath();	
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		var t : String = isDirectory() ? 'Folder' : 'File';
		
		return t + ' [' + _nID + '] Name : ' + getName() + ' | Path : ' + getPath() + ' | Parent : ' + getParentPath();
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */ 
	private function File( pathname : String, isFolder : Boolean ) 
	{
		if( pathname )
		{
			_setFullQualifiedPath( pathname );
		}
		else
		{
			_sName = '';
			_sParentPath = '';
			_sExtension = null;
		}
		
		nameField = _sName;
		setDirectory( isFolder ? true : false );
		_nSize = 0;
		_oData = null;
		
		_nID = HashCodeFactory.getNextKEY();
		
		_register( this );
	}
	
	private function _setFullQualifiedPath( fullName : String ) : Void
	{
		fullName = StringUtil.trim( fullName );
		fullName = StringUtil.rightTrim( fullName, '/' );
		
		var pIndex : Number = fullName.lastIndexOf( '.' );
		if( pIndex > 0 )
		{
			_sExtension = ( String( fullName.split( '.' ).pop() ) );
		}
		else
		{
			_sExtension = null;
			pIndex = fullName.length;
		}
		
		_sName = fullName.substring( fullName.lastIndexOf( '/' ) + 1, pIndex );
		nameField = _sName;
		
		_sParentPath = fullName.substring( 0, fullName.lastIndexOf( '/' ) );
		_sParentPath = StringUtil.startWithChar( _sParentPath, '/', true );
	}
	
	
	
	//-------------------------------------------------------------------------
	// File Manager implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Registers passed-in {@code file}.
	 */
	private static function _register( file : File ) : Void
	{
		_m.put( file.getID(), file );
	}
	
	/**
	 * Unregisters file registred with passed-in {@code key} id.
	 */
	private static function _unregister( key : Number ) : Boolean
	{
		if ( isRegistered( key ) )
		{
			_m.remove( key );
			return true;		
		} else return false;
	}
	
	/**
	 * Returns passed-in {@code fileID} {@link fever.data.file.IFile} 
	 * parent. ( {@code null} if not exist ).
	 */
	private static function _getParent( path : String ) : File
	{
		var iterator : Iterator = _m.getValuesIterator();
		
		while( iterator.hasNext() )
		{
			var o : File = iterator.next();
			if( ( o.getPath() ) == path ) return o;	
		}
		
		return null;
	}
	
	/**
	 * Returns folder content list.
	 */
	private static function _getChildList( file : File ) : Array
	{
		var iterator : Iterator = _m.getValuesIterator();
		var list : Array = new Array();
		
		while( iterator.hasNext() )
		{
			var o : File = iterator.next();
			
			if( o.getID() != file.getID() )
				if( o.getPath().indexOf( file.getPath() ) == 0 ) list.push( o );
		}
		
		return list;
	}
}